<?php
namespace app\models;
/****
基于gameuser表建立的model类
****/

use yii\db\ActiveRecord;
use app\models\Morning;
/**
 * This is the model class for table "blxb_user".
 *
 * @property integer $id
 * @property string $openid
 * @property string $nickname
 * @property string $avatarUrl
 * @property string $createtime
 * @property string $updatetime
 */
class GameUser extends ActiveRecord
{
    /**
     * @inheritdoc
     */
     public static function tableName(){
        return "{{%gameuser}}";
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['openid', 'nickname'], 'required'],
            [['createtime', 'updatetime'], 'safe'],
            [['openid', 'avatarUrl'], 'string', 'max' => 250],
            [['openid'], 'unique'],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '用户序号',
            'openid' => '用户openid',
            'nickname' => '用户昵称',
            'avatarUrl' => '头像',
            'createtime' => '创建时间',
            'updatetime' => '更新时间',
        ];
    }
    
    //根据openid获取用户的信息
    public static function judgeOpenId($openid){
        $data = GameUser::find()->where('openid = :openid',[':openid'=>$openid] )->asArray()->one();
        return $data;
    }

    //数据库 增和改可能会出错 因此增error=-1 改error=0 
    //判断用户的信息是否有所改变 如头像、昵称、openid...
    public static function judgedata_new($userdata){
        $data = GameUser::find()->where('openid = :openid',[':openid'=>$userdata['openid']] )->asArray()->one();
        if( empty($data) ){
            $result = GameUser::add($userdata);
            $morningdata = GameUser::judgeOpenId($userdata['openid']);
            $save = array(
                'userid' => $morningdata['id'],
                );
            $morning = Morning::add($save);
            if( $result == 1 && $morning == 1 ){
                $result = 1;
            }else{
                $result = -1;
            }
        }else{
            if( $userdata['nickname'] != $data['nickname'] || $userdata['avatarUrl'] != $data['avatarUrl'] ){
                $key = [
                    'openid' => $userdata['openid'],
                ];
                $changedata = [
                    'nickname' => $userdata['nickname'],
                    'avatarUrl' => $userdata['avatarUrl'],
                ];
                $result = GameUser::updateAll($changedata,$key);
                if( $result != 1 ){
                    $result = 0;
                }
            }else{
                $result = 1;
            }
        }
        return $result;
    }

    //根据openid返回用户信息
    public static function data_by_openid($openid){
        $data = -1;
        if( isset($openid) ){
            $data = GameUser::find()->where('openid = :openid',[':openid'=>$openid] )->asArray()->one();
            if( empty($data) ){
                $data = 0;
            }else{
            $data = array(
                'id' => $data['id'],
                'nickname' => $data['nickname'],
                'avatarUrl' => $data['avatarUrl']
                );     
            }
        }
        return $data;
    }

    //根据用户id返回信息
    public static function data_by_id($userid){
        $data =  GameUser::find()->where('id = :userid',[':userid'=>$userid] )->asArray()->one();
        return $data;
    }

    //数据库的写操作
    public static function add($data)
    {
        $gameuser = new GameUser();
        if ($gameuser->load($data,'') && $gameuser->save()) {
            return true;
        }
        return false;
    }
}
